package com.lzy.system.organization.controller;

import com.lzy.conf.JSONResult;
import com.lzy.system.organization.entity.SysRole;
import com.lzy.system.organization.service.ISysRoleService;
import com.lzy.utils.ValidList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色管理
 * </p>
 *
 * @author lcc
 * @since 2022-08-13
 */
@RestController
@RequestMapping("/organization/sysRole")
@Api(tags = "角色管理")
public class SysRoleController {
    ISysRoleService iSysRoleService;

    @Autowired
    public SysRoleController(ISysRoleService iSysRoleService) {
        this.iSysRoleService = iSysRoleService;
    }

    @ApiOperation("根据用户id查询角色列表")
    @RequestMapping(value = "selectByUid", method = RequestMethod.POST)
    public JSONResult selectByUid(@ApiParam("账号") @RequestParam("userId") String userId) throws Exception {
        List<SysRole> sysRoles = iSysRoleService.selectByUserName(userId);
        return JSONResult.ok(sysRoles);
    }

    @ApiOperation("根据id查询角色")
    @RequestMapping(value = "/getById/{id}", method = RequestMethod.GET)
    public JSONResult getById(@ApiParam("角色id") @PathVariable(value = "id", required = true) long id) throws Exception {
        return JSONResult.ok(iSysRoleService.getById(id));
    }

    @ApiOperation("查询角色列表")
    @RequestMapping(value = "getList", method = RequestMethod.POST)
    public JSONResult getList() throws Exception {
        return JSONResult.ok(iSysRoleService.getList());
    }

    @ApiOperation("查询角色目录列表")
    @RequestMapping(value = "getListDir", method = RequestMethod.POST)
    public JSONResult getListDir() throws Exception {
        return JSONResult.ok(iSysRoleService.getListDir());
    }

    @ApiOperation("添加角色")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public JSONResult save(@ApiParam("角色信息") @RequestBody @Validated() SysRole SysRole) throws Exception {
        return JSONResult.ok(iSysRoleService.add(SysRole));
    }

    @ApiOperation("修改角色")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public JSONResult update(@ApiParam("角色信息") @RequestBody @Validated() SysRole SysRole) throws Exception {
        return JSONResult.ok(iSysRoleService.update(SysRole));
    }

    @ApiOperation("删除角色")
    @RequestMapping(value = "delById/{id}", method = RequestMethod.DELETE)
    public JSONResult delById(@ApiParam("角色id") @PathVariable(value = "id", required = true) long id) throws Exception {
        return JSONResult.ok(iSysRoleService.delById(id));
    }

    @ApiOperation("根据映射id查询角色列表")
    @RequestMapping(value = "getByMidSysRoleList/{mid}", method = RequestMethod.GET)
    public JSONResult getByMidSysRoleList(@ApiParam("用户映射id") @PathVariable(value = "mid", required = true) long mid) throws Exception {
        return JSONResult.ok(iSysRoleService.getByMidSysRoleList(mid));
    }

    @ApiOperation("批量删除角色")
    @RequestMapping(value = "delBatch", method = RequestMethod.DELETE)
    public JSONResult del(
            @ApiParam("角色列表") @RequestBody @Validated() ValidList<SysRole> SysRole
    ) throws Exception {
        return JSONResult.ok(iSysRoleService.delBatch(SysRole));
    }
}
